package com.anolesoft.yr.demo.controller;

import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.demo.BaseQueryParamVO;
import com.anolesoft.yr.demo.PageResult;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author zhj
 * @date 2021/7/22 14:20
 */
@RestController
@RequestMapping("/api/demo")
public class DemoUserController {
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    @ToString(callSuper = true)
    @EqualsAndHashCode(callSuper=true)
    static class DemoVO extends BaseQueryParamVO implements Serializable {
        private Integer no;
        private String description;
        private Integer callNo;
        private Integer status;
        private String updatedAt;
        private String id;
    }

    private List<DemoVO> getList(){
        return IntStream.rangeClosed(0,210).boxed().map(t->DemoVO.builder()
                .no(t).description("desc"+t).callNo(1).status(t.intValue()%3).updatedAt("").id(t.toString()).key(t.toString())
                .build()).collect(Collectors.toList());
    }

    @GetMapping("/list")
    public AjaxResult list(DemoVO demoVO){
//        System.out.println(demoVO.getPageNo() + "\t" +demoVO.getPageSize());

        List<DemoVO> alllist = this.getList();
        int pn =  demoVO.getPageNo().intValue();
        int ps = demoVO.getPageSize().intValue();
        int endIndex = pn * ps > alllist.size() ? alllist.size() : pn*ps;
        List<DemoVO> list = alllist.subList( (pn-1) *ps, endIndex  );
        PageResult pageResult = new PageResult(list, demoVO.getPageSize(), demoVO.getPageNo(), alllist.size());

//        AjaxResult success = AjaxResult.error("aa");
        AjaxResult success = AjaxResult.success();
        success.put("result", pageResult);
//        System.out.println(success);
        return success;
    }
}
